using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace Developex.DatabaseWrapper
{
	/// <summary>
	/// Order info
	/// </summary>
	public class OrderInfo
	{
		/// <summary>
		/// Sort direction
		/// </summary>
		public bool SortAsc;
		/// <summary>
		/// Ordering field
		/// </summary>
		public string Field;

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="ascending"></param>
		/// <param name="field"></param>
		public OrderInfo(string field, bool ascending)
		{
			SortAsc = ascending;
			Field = field;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="field"></param>
		/// <param name="sortDirection"></param>
		public OrderInfo(string field, SortDirection sortDirection)
		{
			SortAsc = sortDirection == SortDirection.Ascending;
			Field = field;
		}

		/// <summary>
		/// Constructor (ascending by default)
		/// </summary>
		/// <param name="field"></param>
		public OrderInfo(string field)
		{
			Field = field;
			SortAsc = true;
		}

		/// <summary>
		/// Get order expression
		/// </summary>
		/// <returns></returns>
		public string GetExpression()
		{
			return string.Format("{0} {1}", Field, SortAsc ? "ASC" : "DESC");
		}

		/// <summary>
		/// Get order expression
		/// </summary>
		/// <param name="listOrdering"></param>
		/// <returns></returns>
		public static string GetExpression(List<OrderInfo> listOrdering)
		{
			if (listOrdering == null || listOrdering.Count == 0)
				return "";
			StringBuilder expression = new StringBuilder();
			expression.Append("ORDER BY ");
			for (int i = 0; i < listOrdering.Count; i++)
			{
				if (i > 0)
					expression.Append(",");
				expression.Append(listOrdering[i].GetExpression());
			}
			return expression.ToString();
		}
	}
}
